/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.speakglobe.security.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import com.speakglobe.security.DESEDE;
/**
 *
 * @author ronnie
 */
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "user_type", discriminatorType = DiscriminatorType.STRING)
@Table (name = "user_auth")
public class User implements UserDetails {
	private static final Logger logger = Logger.getLogger(User.class);
    private static final long serialVersionUID = 1L;
    
    public static final String ROLE_PANELIST = "ROLE_PANELIST";
    public static final String ROLE_CLIENT = "ROLE_CLIENT";
    public static final String ROLE_ADMIN = "ROLE_ADMIN";
    public static final String ROLE_USER = "ROLE_USER";
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String username;
	
    private String password;

    private String email;

    @Column(name = "user_role")
    private String userRole;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date updated;
    
    
    
    @Transient
    private GrantedAuthority[] grantedAuthorities;

    private String title;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }
    
    public String getDecryptPassword(){
    	String password = getPassword();
    	String username = getUsername();
    	if (password != null && username != null) {
    		DESEDE desede = new DESEDE(username);
    		password = desede.decrypt(password);
    		return password;
    	}
    	
    	return null;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public void setEncryptPassword(String username, String password){
    	if (password != null && username != null) {
    		DESEDE desede = new DESEDE(username);
    		this.password = desede.encrypt(password);
    	}
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUserRole() {
        return userRole;
    }

    public void setUserRole(String userRole) {
        this.userRole = userRole;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public Collection<GrantedAuthority> getAuthorities() {
		ArrayList authorities = new ArrayList();
		
		GrantedAuthorityImpl authority = new GrantedAuthorityImpl(ROLE_USER);
    	GrantedAuthorityImpl authorityNew = new GrantedAuthorityImpl(ROLE_PANELIST);
    	
    	authorities.add(authority);
    	authorities.add(authorityNew);
    	
    	return authorities;
    }

    @Override
    public String toString() {
        return "com.speakglobe.security.model.User[ id=" + id + " ]";
    }

    public boolean hasRole(String role) {
    	Collection<GrantedAuthority> authorities = this.getAuthorities();
    	for(GrantedAuthority authority : authorities) {
    		logger.debug("The authority &&&&& " + authority.getAuthority());
    		if(authority.getAuthority().equals(role)) {
    			return true;
    		}
    	}
    	
    	return false;
    }

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return false;
	}
    
}
